import { Login } from '@/common/api'
import { address, paySet, WeixinLogin } from '@/common/api/users'

const user = {
	state: {
		userInfo: 0,
		address: [],
		isUpdate: false,
		location: {
			latitude: 23.02067,
			longitude: 13.75179
		},
		userCity: {}
	},
	mutations: {
		SET_USERCITY: (state, userCity) => {
			state.userCity = userCity
		},
		SET_ISUPDATE: (state, isUpdate) => {
			state.isUpdate = isUpdate
		},
		SET_LOCATION: (state, location) => {
			state.location = location
		},
		SET_USERINFO: (state, userInfo) => {
			state.userInfo = userInfo
		},
		SET_ADDRESS: (state, address) => {
			state.address = address
		},
		SET_PAYPWD: (state, pay_pwd) => {
			state.userInfo.pay_pwd = pay_pwd
		},
		SET_RANK: (state, rank) => {
			state.userInfo.rank = rank
		},
		SET_PHONE: (state, phone) => {
			state.userInfo.phone = phone
		}		
	},
	actions: {
		   async userLogin ({ commit, dispatch, state }, info) { // 手机号码登录
			 let userInfo = {}		   
			 await dispatch('uLogin', { userInfo, info })
			 await dispatch('getAddress', { ticket: userInfo.ticket, uname: userInfo.uname, user_id: userInfo.id })
			 const pay_pwd = await dispatch('getPayPwd', { user_id: userInfo.id, ticket: userInfo.ticket })
			 userInfo.pay_pwd = pay_pwd
			 commit('SET_USERINFO', userInfo)
			 uni.setStorageSync('userInfo', userInfo)
			 uni.switchTab({ url: '/pages/tabBar/usercenter/index' })				 
		},
		logout ({ commit }) { // 退出登录
			uni.showModal({
				title: '',
				content: '确认退出登录吗?',
				success: res => {
					if (res.confirm) {
						uni.setStorageSync('userInfo', 0)
						//uni.setStorageSync('shop', 0)
						commit('SET_USERINFO', 0)
						uni.switchTab({
							url: '/pages/tabBar/usercenter/index'
						})
					}
				}
			})
		},
		uLogin ({ }, { userInfo, info }) {
			return new Promise(resolve => {
				Login(info).then(res => {
					if (res.status) {
						const data = res.data
						userInfo.login = info
						userInfo.nickName = data.information.weixin_name !== null && data.information.weixin_name !== 'null' ? data.information.weixin_name : data.information.name
						userInfo.phone = data.information.phone
						userInfo.avatarUrl = data.information.weixin_headimgurl || '/static/profile.png'
						userInfo.id = data.information.user_id
						userInfo.ticket = data.ticket
						userInfo.uname = data.uname
						userInfo.rank = data.cardEnti
						resolve()
					} else {
						uni.showModal({
							showCancel: false,
							content: '用户名或密码错误'
						})
					}
				 })
			})
		},
		getAddress ({ commit }, data) { // 用户地址
			return new Promise(resolve => {
				address(data).then(res => {
					commit('SET_ADDRESS', res.data || [])
					uni.setStorageSync('address', res.data || [])
					if (res.code === 400000) {
						resolve(res.data)
					} else {
						resolve([])
					}
				})
			})
		},
		getPayPwd ({}, data) { // 内部支付密码
			return new Promise(resolve => {
				paySet(data).then(res => {
					if (res.code) {
						resolve(res.mge)
					} else {
						resolve(0)
					}
				})
			})
		},
		async wxLogin ({ dispatch, commit }, db) {
			let userInfo = {}
			let wxInfo = await dispatch('WeiXinLogin', db)
			userInfo = Object.assign({}, userInfo, wxInfo)
			uni.showLoading({ title: '登录中' })
			 await dispatch('getAddress', { ticket: userInfo.ticket, uname: userInfo.uname, user_id: userInfo.id })
			 const pay_pwd = await dispatch('getPayPwd', { user_id: userInfo.id, ticket: userInfo.ticket })
			 userInfo.pay_pwd = pay_pwd
			commit('SET_USERINFO', userInfo)
			uni.setStorageSync('userInfo', userInfo)
			uni.switchTab({ url: '/pages/tabBar/usercenter/index' })
			uni.hideLoading()
		},
		WeiXinLogin ({}, db) {
			return new Promise(resolve => {
				uni.getProvider({
					service: 'oauth',
					success: res => {
						if (~res.provider.indexOf('weixin')) {
							uni.login({
								provider: 'weixin',
								success: rst => {
									uni.getUserInfo({
										provider: 'weixin',
										success: info => {
											let params = {
												...info.userInfo,
												referrer: db.referrer
											}
											uni.showLoading()
											WeixinLogin(params).then(result => {
												uni.hideLoading()
												if (result.status) {
													const data = result.data
													let userInfo = {
														...info.userInfo
													}
													userInfo.nickName = data.information.weixin_name
													userInfo.phone = data.information.phone
													userInfo.avatarUrl = data.information.weixin_headimgurl || '/static/profile.png'
													userInfo.id = data.information.user_id
													userInfo.ticket = data.ticket
													userInfo.uname = data.uname
													userInfo.rank = data.cardEnti
													resolve(userInfo)
												} else {
													uni.showModal({
														content: result.msg,
														showCancel: false,
														success: res => {
															if (res.confirm) {
																db.callback(true)
															}
														}
													})
												}
											})
										}
									})
								}
							})
						}
					}
				})				
			})
		}		
	}
}

export default user